package org.diysearch.tests;

import java.util.List;

import junit.framework.TestCase;

import org.diysearch.helpers.CommentHelper;
import org.diysearch.helpers.LinkHelper;
import org.diysearch.helpers.TagHelper;
import org.diysearch.interfaces.Constants;
import org.diysearch.models.Comment;
import org.diysearch.models.Link;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/**
 * Test the link functions -- it is important to note that these do not go through the
 * actual service classes, and use the Helper classes directly (just as the service 
 * classes shall)
 * 
 * @author dpalmer
 *
 */
public class LinkTests extends TestCase {

	private MockHttpServletRequest request = new MockHttpServletRequest();
	private MockHttpServletResponse response = new MockHttpServletResponse();
	static int URL_ID = 0;
	protected void setUp() throws Exception {
		super.setUp();
	}
	
	public void testAddUrl() {
		Link link = new Link();
		link.setDescription("test link: " +System.currentTimeMillis());
		link.setUrl("http://www.diysearch.com/"+System.currentTimeMillis());
		link.setIsActive(0);
		link.setEditorPick(0);
		link.setOwner(999); // just a bogus user
		link.setTitle("TEST LINK");
		link.setCategory(1); // dummy category
		
		try {
			int code = LinkHelper.save(link);
			URL_ID = code;
			System.out.println("New link ID: " + code);
			assertEquals("code greater than 0",1,code);
		} catch (Exception ex) {
			ex.printStackTrace(System.err);
			fail("couldn't save a link");
		}
		
	}
	
	public void testGetUrl() {
		try {
			Link l = LinkHelper.fetchLink(7,1);
			System.out.println(l);
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("couldn't fetch a stinkin link");
		}
	}
	
	public void testGetUrls() {
		try {
			String[] args = {"-rt","0","-lmt","10"};
			List<Link> results = LinkHelper.fetchLinks(args);
			assertNotNull("results list",results);
			
			for (Link link : results) {
				System.out.println(link);
			}
			
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("couldn't fetch a shit load of links, fail");
		}
	}
	
	public void testDeleteUrl() {
		if (URL_ID > 0) {
			try {
				int code = LinkHelper.deleteLinkById(URL_ID);
				System.out.println("deleted: " + code + " url");
			} catch (Exception ex) {
				ex.printStackTrace(System.out);
				fail("Could not delete URL: " + URL_ID);
			}
		} else {
			System.out.println("URL_ID was 0");
		}
	}
	
	public void testUpdateUrl() {
		Link link = new Link();
		link.setId(11);
		link.setUrl("http://changed.diysearch.com/"+ System.currentTimeMillis());
		link.setDescription("i just changed this URL");
		link.setTitle("Update Title");
		link.setCategory(1);
		
		try {
			int code = LinkHelper.save(link);
			System.out.println ("Update link returned code: " + code);
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("couldn't update a link");
		}
	}
	
	public void testTagUrl() {
		int item = 7;
		int uid = 1;
		int oid = Constants.LINKS_OBJECT_ID;
		String tag ="diy";
		
		try {
			int code = TagHelper.save(tag,uid,oid,item);
			System.out.println ("Saving a tag returned: " + code);
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("couldn't tag a url");
		}
	}
	
	public void testCommentUrl() {
		Comment comment = new Comment();
		comment.setBody("this is a test comment and its freakin awesome");
		comment.setItem(7);
		comment.setObjectId(Constants.LINKS_OBJECT_ID);
		comment.setOwner(1);
		comment.setSubject("test comment: " + System.currentTimeMillis());
		comment.setParentId(0);
		
		try {
			Comment c = CommentHelper.save(comment);
			assertNotNull("returned comment",c);
			System.out.println("new comment ID: " + c.getId());
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			fail("couldn't save a comment");
		}
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

}
